﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Weblications.Sango.Protocol.world;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Framework.Verifications;
using Pixysoft.Weblications.Sango.Protocol.player;
using System.Data;
using Pixysoft.Weblications.Sango.Protocol.section;
using Pixysoft.Weblications.Sango.Battle;
using Pixysoft.Weblications.Sango.Dao;

namespace Pixysoft.Weblications.Sango.Protocol
{
    class UsrSectionFacade
    {
        public static world_nation ToNation(Entities.Cti_Nation orm)
        {
            Entities.Sys_Nation sysNation = SysNationHelper.search(orm.Nationtypecode);

            world_nation body = ReflectionManager.CreatePojo<world_nation>();
            body.nationtypecode = orm.Nationtypecode;
            body.name = sysNation.Nationname;
            body.population = orm.Populationvalue;
            body.prosperity = orm.Prosperityvalue;
            return body;

        }

        public static section_area ToSectionArea(Entities.Sys_Section sys)
        {
            if (sys == null)
                return ToSectionArea();

            Entities.Cti_Section orm = CitySectionHelper.searchSectionByCode(sys.Sectionserialcode);
            section_area body = ReflectionManager.CreatePojo<section_area>();
            body.nationname = sys.Nationname;
            body.sectionname = sys.Sectionname;
            body.nationtypecode = orm.Belongnationtypecode;
            body.sectionserialcode = sys.Sectionserialcode;
            body.sectiontypecode = sys.Sectiontypecode;
            body.population = orm.Populationvalue;
            body.prosperity = orm.Prosperityvalue;
            return body;
        }

        protected static section_area ToSectionArea()
        {
            Entities.Sys_Section sys = SysSectionHelper.trySearchSectionByGeolocation(-1, -1);
            if (sys == null)
                throw Exceptions.VerificationFailedException();

            section_area body = ReflectionManager.CreatePojo<section_area>();
            body.nationname = sys.Nationname;
            body.sectionname = sys.Sectionname;
            body.nationtypecode = sys.Nationtypecode;
            body.sectionserialcode = sys.Sectionserialcode;
            body.population = 0;
            body.prosperity = 0;
            return body;
        }

        public static player_section ToSection(Entities.Usr_Section orm)
        {
            Entities.Sys_Section section = SysSectionHelper.searchSectionByCode(orm.Sectionserialcode);
            Entities.Sys_Nation nation = SysNationHelper.search(orm.Nationtypecode);
            Entities.Cti_Section ctiSection = CitySectionHelper.searchSectionByCode(section.Sectionserialcode);

            player_section body = ReflectionManager.CreatePojo<player_section>();
            body.nationtypecode = nation.Nationtypecode;
            body.sectionserialcode = section.Sectionserialcode;
            body.sectiontypecode = section.Sectiontypecode;
            body.terriantypecode = section.Terriantypecode;
            body.avatarindex = section.Avatarindex;
            body.gradetypecode = section.Gradetypecode;

            body.nationname = nation.Nationname;
            body.sectionname = section.Sectionname;
            body.sectiontypename = section.Sectiontypename;

            body.population = ctiSection.Populationvalue;
            body.prosperity = ctiSection.Prosperityvalue;
            body.belongnationname = SysNationHelper.search(ctiSection.Belongnationtypecode).Nationname;
            body.nationholdvalue = SectionFormular.GetNationHoldValue(orm.Nationtypecode, ctiSection);


            return body;
        }

        public static player_section ToSection(Entities.Cti_Section ctiSection, Entities.Usr_Section orm)
        {
            Entities.Sys_Section section = SysSectionHelper.searchSectionByCode(orm.Sectionserialcode);
            Entities.Sys_Nation nation = SysNationHelper.search(orm.Nationtypecode);

            player_section body = ReflectionManager.CreatePojo<player_section>();
            body.nationtypecode = nation.Nationtypecode;
            body.sectionserialcode = section.Sectionserialcode;
            body.sectiontypecode = section.Sectiontypecode;
            body.terriantypecode = section.Terriantypecode;
            body.avatarindex = section.Avatarindex;
            body.gradetypecode = section.Gradetypecode;

            body.nationname = nation.Nationname;
            body.sectionname = section.Sectionname;
            body.sectiontypename = section.Sectiontypename;

            body.population = ctiSection.Populationvalue;
            body.prosperity = ctiSection.Prosperityvalue;
            body.belongnationname = SysNationHelper.search(ctiSection.Belongnationtypecode).Nationname;
            body.nationholdvalue = SectionFormular.GetNationHoldValue(orm.Nationtypecode, ctiSection);

            return body;
        }

        public static List<player_section> ToSections(List<Entities.Cti_Section> ctiSections)
        {
            List<player_section> list = new List<player_section>();

            foreach (Entities.Cti_Section orm in ctiSections)
            {
                Entities.Sys_Section sys = SysSectionHelper.searchSectionByCode(orm.Sectionserialcode);
                list.Add(ToSection(sys, orm));
            }

            return list;
        }

        public static player_section ToSection(Entities.Sys_Section sysSection, Entities.Cti_Section ctiSection)
        {
            Entities.Sys_Nation nation = SysNationHelper.search(ctiSection.Initializenationtypecode);

            player_section body = ReflectionManager.CreatePojo<player_section>();
            body.nationtypecode = nation.Nationtypecode;
            body.nationname = nation.Nationname;
            body.sectionserialcode = sysSection.Sectionserialcode;
            body.terriantypecode = sysSection.Terriantypecode;
            body.sectiontypecode = sysSection.Sectiontypecode;
            body.sectionname = sysSection.Sectionname;
            body.avatarindex = sysSection.Avatarindex;
            body.gradetypecode = sysSection.Gradetypecode;

            body.population = ctiSection.Populationvalue;
            body.prosperity = ctiSection.Prosperityvalue;
            body.belongnationname = nation.Nationname;
            body.nationholdvalue = SectionFormular.GetNationHoldValue(ctiSection.Belongnationtypecode, ctiSection);

            return body;
        }

        internal static section_resource ToResource(Entities.Usr_Resource orm)
        {
            Entities.Cti_Resource sys = CtiResourceHelper.searchByCode(orm.Resourceserialcode);

            section_resource body = ReflectionManager.CreatePojo<section_resource>();
            body.name = sys.Resourcename;
            body.positionindex = sys.Positionindex;
            body.level = sys.Resourcelevel;
            body.productivitytypecode = sys.Sectionscaletypecode;
            body.productivity = ResourceFormular.GetResourceProductivityValue(sys.Resourcelevel);
            body.occupy = true;
            body.usercode = orm.Usercode;
            body.nickname = orm.Nickname;
            body.enddate = (long)orm.Enddate;
            body.primarycode = orm.Primarycode;

            return body;
        }

        public static section_resource ToResource(Entities.Cti_Resource orm)
        {
            Entities.Cti_Resource sys = CtiResourceHelper.searchByCode(orm.Resourceserialcode);

            section_resource body = ReflectionManager.CreatePojo<section_resource>();
            body.name = sys.Resourcename;
            body.positionindex = sys.Positionindex;
            body.level = sys.Resourcelevel;
            body.productivitytypecode = sys.Sectionscaletypecode;
            body.productivity = ResourceFormular.GetResourceProductivityValue(sys.Resourcelevel);
            body.occupy = false;

            return body;
        }


        public static section_profile ToProfileByProfile(Entities.Usr_Profile profile)
        {
            section_profile body = ReflectionManager.CreatePojo<section_profile>();
            body.usercode = profile.Usercode;
            body.nickname = profile.Nickname;
            body.avatar = profile.Avatartypecode;
            body.userlevel = profile.Userlevel;
            body.viplevel = profile.Viplevel;
            return body;
        }

        internal static List<section.section_profile> ToProfilesBySection(System.Data.DataTable dataTable)
        {
            List<section_profile> list = new List<section_profile>();
            foreach (DataRow row in dataTable.Rows)
            {
                section_profile body = ReflectionManager.CreatePojo<section_profile>();
                list.Add(body);

                body.usercode = row[Schemas.LOCAL.USR_PROFILE.USERCODE.Column].ToString();
                body.nickname = row[Schemas.LOCAL.USR_PROFILE.NICKNAME.Column].ToString();
                body.avatar = int.Parse(row[Schemas.LOCAL.USR_PROFILE.AVATARTYPECODE.Column].ToString());
                body.userlevel = int.Parse(row[Schemas.LOCAL.USR_PROFILE.USERLEVEL.Column].ToString());
                body.viplevel = int.Parse(row[Schemas.LOCAL.USR_PROFILE.VIPLEVEL.Column].ToString());
                body.fametitlename = row[Schemas.LOCAL.USR_PROPERTY.FAMETITLENAME.Column].ToString();
            }
            return list;
        }

        protected static List<section.section_profile> ToProfilesBySectionBattle(DataTable dataTable)
        {
            List<section_profile> list = new List<section_profile>();
            foreach (DataRow row in dataTable.Rows)
            {
                section_profile body = ReflectionManager.CreatePojo<section_profile>();
                list.Add(body);

                body.usercode = row[Schemas.LOCAL.USR_PROFILE.USERCODE.Column].ToString();
                body.nickname = row[Schemas.LOCAL.USR_PROFILE.NICKNAME.Column].ToString();
                body.avatar = int.Parse(row[Schemas.LOCAL.USR_PROFILE.AVATARTYPECODE.Column].ToString());
                body.userlevel = int.Parse(row[Schemas.LOCAL.USR_PROFILE.USERLEVEL.Column].ToString());
                body.viplevel = int.Parse(row[Schemas.LOCAL.USR_PROFILE.VIPLEVEL.Column].ToString());
            }
            return list;
        }

        internal static List<section_profile> ToProfilesByRveStage(RveStageTeam team)
        {
            List<section_profile> list = new List<section_profile>();
            foreach (string usercode in team.Profiles)
            {
                section_profile body = ToProfileByProfile(UsrProfileDao.getUsrProfile(usercode));
                list.Add(body);
            }
            return list;
        }

        internal static List<section_profile> ToProfilesBySectionTeam(RvrSectionTeam rvrTeam)
        {
            List<section_profile> list = new List<section_profile>();
            foreach (string usercode in rvrTeam.Profiles)
            {
                section_profile body = ToProfileByProfile(UsrProfileDao.getUsrProfile(usercode));
                list.Add(body);
            }
            return list;
        }

        public static List<section_rvrbattle> ToSectionBattles(List<Entities.Usr_Sectionbattle> orms)
        {
            List<section_rvrbattle> list = new List<section_rvrbattle>();

            foreach (Entities.Usr_Sectionbattle orm in orms)
            {
                list.Add(ToSectionBattle(orm));
            }

            return list;
        }

        public static section_rvrbattle ToSectionBattle(Entities.Usr_Sectionbattle orm)
        {
            Entities.Sys_Section section = SysSectionHelper.searchSectionByCode(orm.Sectionserialcode);
            Entities.Sys_Section targetsection = SysSectionHelper.searchSectionByCode(orm.Targetsectionserialcode);

            section_rvrbattle body = ReflectionManager.CreatePojo<section_rvrbattle>();
            body.code = orm.Primarycode;

            body.nationname = section.Nationname;
            body.sectionname = section.Sectionname;
            body.targetnationname = targetsection.Nationname;
            body.targetsectionname = targetsection.Sectionname;
            body.famevalue = orm.Famevalue;
            body.fightdate = (long)orm.Fightdate;
            body.fightresult = SettingHelper.BooleanValue(orm.Fightresult);

            body.sectionserialcode = orm.Sectionserialcode;

            body.targetsectionserialcode = orm.Targetsectionserialcode;
            body.gradetypecode = targetsection.Gradetypecode;
            body.avatarindex = targetsection.Avatarindex;
            return body;
        }
    }
}
